package com.banban.user.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;


@Configuration
@Slf4j
public class SmsConfig {

    public static String signName = "北京伴伴心语科技";
    public static String templateCode = "SMS_315750373";

    public static String accessKeyId = "LTAI5tM6gANND8mArZAMpV9E";
    public static String accessKeySecret = "5ypJgbAbpEuFsKsDKOQTvtzV1Mu42G";
    public static String endpoint = "dysmsapi.aliyuncs.com";
    public static Client createClient() throws Exception {
        Config config = new Config()
                // 配置 AccessKey ID，请确保代码运行环境设置了环境变量。
                .setAccessKeyId(accessKeyId)
                // 配置 AccessKey Secret，请确保代码运行环境设置了环境变量。
                .setAccessKeySecret(accessKeySecret);
        // System.getenv()方法表示获取系统环境变量，请配置环境变量后，在此填入环境变量名称，不要直接填入AccessKey信息。

        // 配置 Endpoint
        config.endpoint = endpoint;

        return new Client(config);
    }


    public static void main(String[] args) throws Exception {
        System.out.println(sendSms("17886649949", "5678"));
    }

    public static String sendSms(String phone, String code) throws Exception {
        // 创建客户端
        Client client = SmsConfig.createClient();
        // 创建请求
        SendSmsRequest request = new SendSmsRequest()
                .setPhoneNumbers(phone)
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setTemplateParam("{\"code\":\"" + code + "\"}");
        // 发送请求
        SendSmsResponse response = client.sendSms(request);
        log.info("send message phone :{} ,code:{} result :{}", phone ,code ,response.body.message);
        return response.body.message;
    }
}
